/**
 *Copyright (c) 2024 watereyes
 * workflow is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.workflow.applicationManag.service.impl;

import com.koron.workflow.applicationManag.bean.WhiteListBean;
import com.koron.workflow.applicationManag.bean.WhiteListQueryBean;
import com.koron.workflow.applicationManag.mapper.WhiteListMapper;
import com.koron.workflow.applicationManag.service.WhiteListService;
import com.koron.workflow.common.bean.PageBean;
import com.koron.workflow.common.bean.StaffBean;
import com.koron.workflow.common.util.BusinessCode;
import com.koron.workflow.common.util.DisposeBeanUtil;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.swan.bean.MessageBean;

import java.util.List;

@Service
public class WhiteListServiceImpl implements WhiteListService {
    private static final Logger logger = LoggerFactory.getLogger(WhiteListServiceImpl.class);

    @TaskAnnotation("queryForList")
    @Override
    public PageBean<WhiteListQueryBean> queryForList(SessionFactory factory, String appId, Integer page, Integer pageSize) {
        WhiteListMapper mapper = factory.getMapper(WhiteListMapper.class);
        List<WhiteListQueryBean> list = mapper.queryForList(appId, page, pageSize);
        Integer total = null;
        if (page != null || pageSize != null){
            total = mapper.countQueryForList(appId);
        }
        return new PageBean<>(list, total);
    }

    @TaskAnnotation("queryForObject")
    @Override
    public WhiteListQueryBean queryForObject(SessionFactory factory, String id) {
        return factory.getMapper(WhiteListMapper.class).queryForObject(id);
    }

    @TaskAnnotation("save")
    @Override
    public MessageBean save(SessionFactory factory, StaffBean user, WhiteListBean bean) {
        MessageBean msg = MessageBean.create(BusinessCode.SUCCESS.getCode(), BusinessCode.SUCCESS.getDescription(), void.class);
        try {
            if (StringUtils.isEmpty(bean.getAppId()) || StringUtils.isEmpty(bean.getServerIP()) ||
                    StringUtils.isEmpty(bean.getServerMAC()) || StringUtils.isEmpty(bean.getServername())) {
                msg.setCode(BusinessCode.EMPTY_PARAM.getCode());
                msg.setDescription(BusinessCode.EMPTY_PARAM.getDescription());
                return msg;
            }
            DisposeBeanUtil.disposeInsert(user, bean);
            if (factory.getMapper(WhiteListMapper.class).insert(bean) != 1) {
                factory.close(false);
                msg.setCode(BusinessCode.FAILURE.getCode());
                msg.setDescription(BusinessCode.FAILURE.getDescription());
                return msg;
            }
        } catch (Exception e) {
            factory.close(false);
            logger.error("白名单保存失败", e);
            msg.setCode(BusinessCode.EXCEPTION.getCode());
            msg.setDescription(BusinessCode.EXCEPTION.getDescription());
        }
        return msg;
    }

    @TaskAnnotation("update")
    @Override
    public MessageBean update(SessionFactory factory, StaffBean user, WhiteListBean bean) {
        MessageBean msg = MessageBean.create(BusinessCode.SUCCESS.getCode(), BusinessCode.SUCCESS.getDescription(), void.class);
        try {
            if (StringUtils.isEmpty(bean.getId())){
                msg.setCode(BusinessCode.EMPTY_PARAM.getCode());
                msg.setDescription("没有获取到当前业务ID");
                return msg;
            }
            if (StringUtils.isEmpty(bean.getAppId()) || StringUtils.isEmpty(bean.getServerIP()) ||
                    StringUtils.isEmpty(bean.getServerMAC()) || StringUtils.isEmpty(bean.getServername())) {
                msg.setCode(BusinessCode.EMPTY_PARAM.getCode());
                msg.setDescription(BusinessCode.EMPTY_PARAM.getDescription());
                return msg;
            }
            DisposeBeanUtil.disposeUpdate(user, bean);
            if (factory.getMapper(WhiteListMapper.class).update(bean) != 1){
                factory.close(false);
                msg.setCode(BusinessCode.FAILURE.getCode());
                msg.setDescription(BusinessCode.FAILURE.getDescription());
                return msg;
            }
        }catch (Exception e){
            factory.close(false);
            logger.error("白名单修改失败", e);
            msg.setCode(BusinessCode.EXCEPTION.getCode());
            msg.setDescription(BusinessCode.EXCEPTION.getDescription());
        }
        return msg;
    }

    @TaskAnnotation("delete")
    @Override
    public MessageBean delete(SessionFactory factory, String id) {
        MessageBean msg = MessageBean.create(BusinessCode.SUCCESS.getCode(), BusinessCode.SUCCESS.getDescription(), void.class);
        try {
            if (factory.getMapper(WhiteListMapper.class).delete(id) != 1){
                factory.close(false);
                msg.setCode(BusinessCode.FAILURE.getCode());
                msg.setDescription(BusinessCode.FAILURE.getDescription());
                return msg;
            }
        }catch (Exception e){
            logger.error("白名单删除失败", e);
            factory.close(false);
            msg.setCode(BusinessCode.EXCEPTION.getCode());
            msg.setDescription(BusinessCode.EXCEPTION.getDescription());
        }
        return msg;
    }
}
